var mongoose = require('mongoose'),
    uuid = require('uuid'),
    Schema = mongoose.Schema;
var orderItem = new Schema({
    name: {type: String},
    img: {type: String},
    price: {type: Number},
    count: {type: Number}
});
var OrderSchema = new Schema({
    order_id: {type: String},
    merchant_id: {type: String},
    storeName :{type:String},
    customer_id: {type: String},
    customer_name: {type: String},
    customer_headpic: {type: String},
    total: {type: Number},
    telephone:{type:String},
    address: {type: String},
    state: {type: Number},
    items: {type:[orderItem]}
}, {
    collection: 'order'
});


// Indexes

// Auto update last update time
OrderSchema.pre('update', function () {
    // next();
});

OrderSchema.pre('save', function (next) {
    var order = this;
    order.order_id = uuid.v4();
    order.state = 0;
    next();
});

mongoose.model('Order', OrderSchema);